package com.hzzc.api;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户对于单个应用的权限视图
 *
 * @author zmm
 */
public class SSOApplicationUserAuthorityView {

    /**
     * 用户
     */
    private SSOUserView ssoUserView;

    /**
     * 应用
     */
    private SSOApplicationView ssoApplication;

    /**
     * 资源树状视图
     */
    private List<SSOResourceTreeView> resourceTreeViews;

    /**
     * 资源定位符
     */
    private Set<String> resourcesLocators;


    /**
     * 资源定位符
     */
    private Set<String> resourcesCodes;


    public SSOApplicationUserAuthorityView() {
    }

    /**
     * 构造视图
     *
     * @param ssoApplication
     * @param ssoUserView
     * @param ssoResources
     */
    public SSOApplicationUserAuthorityView(SSOApplicationView ssoApplication, SSOUserView ssoUserView, List<SSOResourceView> ssoResources, List<SSOResourceTreeView> resourceTreeViews) {
        this.ssoUserView = ssoUserView;
        this.ssoApplication = ssoApplication;
        this.resourceTreeViews = resourceTreeViews;
        this.resourcesCodes = initResourcesCodes(ssoResources);
        this.resourcesLocators = initResourcesLocators(ssoResources);
    }

    public Set<String> getResourcesCodes() {
        return resourcesCodes;
    }

    public SSOApplicationUserAuthorityView setResourcesCodes(Set<String> resourcesCodes) {
        this.resourcesCodes = resourcesCodes;
        return this;
    }

    private Set<String> initResourcesCodes(List<SSOResourceView> ssoResources) {
        return ssoResources.stream().map(SSOResourceView::getCode).collect(Collectors.toSet());
    }

    /**
     * @param ssoResources
     * @return
     */
    private Set<String> initResourcesLocators(List<SSOResourceView> ssoResources) {
        return ssoResources.stream().map(SSOResourceView::getLocator).collect(Collectors.toSet());
    }


    public SSOUserView getSsoUserView() {
        return ssoUserView;
    }

    public SSOApplicationUserAuthorityView setSsoUserView(SSOUserView ssoUserView) {
        this.ssoUserView = ssoUserView;
        return this;
    }

    public SSOApplicationView getSsoApplication() {
        return ssoApplication;
    }

    public SSOApplicationUserAuthorityView setSsoApplication(SSOApplicationView ssoApplication) {
        this.ssoApplication = ssoApplication;
        return this;
    }

    public List<SSOResourceTreeView> getResourceTreeViews() {
        return resourceTreeViews;
    }

    public SSOApplicationUserAuthorityView setResourceTreeViews(List<SSOResourceTreeView> resourceTreeViews) {
        this.resourceTreeViews = resourceTreeViews;
        return this;
    }

    public Set<String> getResourcesLocators() {
        return resourcesLocators;
    }

    public SSOApplicationUserAuthorityView setResourcesLocators(Set<String> resourcesLocators) {
        this.resourcesLocators = resourcesLocators;
        return this;
    }

}
